package com.ydgk.servlet;

import com.alibaba.fastjson.JSON;
import com.ydgk.bean.Commodity;
import com.ydgk.bean.Provider;
import com.ydgk.dao.ProviderDao;
import com.ydgk.dao.UserDao;
import com.ydgk.service.CommService;
import com.ydgk.service.ProviderService;
import com.ydgk.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet(name = "ProviderServlet", urlPatterns = "/ProviderServlet.action")
public class ProviderServlet extends BaseServlet {

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Provider provider = new Provider();
        provider.setPname(request.getParameter("pname"));
        provider.setPeople(request.getParameter("people"));
        provider.setAddress(request.getParameter("address"));
        provider.setPmoney(Integer.parseInt(request.getParameter("pmoney")));
        provider.setPhone(Integer.parseInt(request.getParameter("phone")));
        ProviderService providerService = new ProviderService();
        int i=providerService.add(provider);
        response.getWriter().print(i>0?"true":"false");
    }


    protected void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        int pid= Integer.parseInt(request.getParameter("pid"));
        System.out.println(pid);
        ProviderService providerService = new ProviderService();
        int i=providerService.deleteById(pid);
        HashMap map = new HashMap();
        if(i>0){
            map.put("flag","true");
        }else{
            map.put("flag","false");
        }
        response.getWriter().print(JSON.toJSONString(map));
    }

    protected void updateById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        int pid= Integer.parseInt(request.getParameter("pid"));
        int phone= Integer.parseInt(request.getParameter("phone"));
        String pname=request.getParameter("pname");
        String people=request.getParameter("people");
        String address=request.getParameter("address");
        Provider provider=new Provider();
        provider.setPid(pid);
        provider.setPname(pname);
        provider.setPeople(people);
        provider.setPhone(phone);
        provider.setAddress(address);
        ProviderService providerService = new ProviderService();
        int i=providerService.updateById(provider);
        HashMap map = new HashMap();
        if(i>0){
            map.put("flag","true");
        }else{
            map.put("flag","false");
        }
        response.getWriter().print(JSON.toJSONString(map));
    }

    protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        ProviderService providerService = new ProviderService();
        List<Provider> providerList = providerService.findAll();
        request.setAttribute("providerList",providerList);
        request.setAttribute("mainPage","providerFindAll.jsp");
        request.getRequestDispatcher("index.jsp").include(request,response);
    }

    protected void findProviderAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("application/json;charset=utf-8");
        ProviderService providerService = new ProviderService();
        int maxPage = providerService.maxPage("provider");
        String pn = request.getParameter("pageNumber");
        String pc = request.getParameter("pageCount");
        int pageNumber = 1;
        int pageCount = 8 ;
        try {
            pageNumber = Integer.parseInt(pn);
            pageCount = Integer.parseInt(pc);
        }catch (NumberFormatException e){
            pageNumber = 1;
            pageCount = 8 ;
        }
        ProviderDao providerDao = new ProviderDao();

        providerDao.pageCount = pageCount;
        if(pageNumber <1){
            pageNumber=1;
        }

        if(pageNumber > maxPage){
            pageNumber = maxPage ;
        }

        List<Provider> providerList = providerService.paging(pageNumber);
        int total = providerService.total();
        HashMap map = new HashMap() ;

        map.put("code",0);
        map.put("msg","");
        map.put("data",providerList);
        map.put("pageCount",pageCount);
        map.put("pageNumber",pageNumber);
        map.put("total",total);

        String str = JSON.toJSONString(map);

        response.getWriter().print(str);

    }

    protected void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        ProviderService providerService=new ProviderService();
        int id= Integer.parseInt(request.getParameter("id"));
        Provider provider=providerService.findById(id);
        request.setAttribute("pro",provider);
        request.getRequestDispatcher("providerDeatil.jsp").forward(request,response);
    }

    protected void findAllProvider(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ProviderService providerService = new ProviderService();
        response.getWriter().print(JSON.toJSONString(providerService.findAll()));
    }

}
